package com.zhoukai.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhoukai.constants.Constants;
import com.zhoukai.dto.MedicinesDto;
import com.zhoukai.vo.DataGridView;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhoukai.mapper.MedicinesMapper;
import com.zhoukai.domain.Medicines;
import com.zhoukai.service.MedicinesService;
/**
 * Created with IntelliJ IDEA.
 * @Author: zk
 * @Date: 2022/02/09/8:58
 * @Description: 
 */
@DubboService(methods = {@Method(name = "addMedicines",retries = 0)})
public class MedicinesServiceImpl implements MedicinesService{

    @Autowired
    private MedicinesMapper medicinesMapper;
    @Override
    public DataGridView listMedicinesPage(MedicinesDto medicinesDto) {
        Page<Medicines> page = new Page<>(medicinesDto.getPageNum(), medicinesDto.getPageSize());
        this.medicinesMapper.selectPage(page, Wrappers.lambdaQuery(Medicines.class)
            .like(StringUtils.isNotBlank(medicinesDto.getMedicinesName()),Medicines::getMedicinesName,medicinesDto.getMedicinesName())
            .like(StringUtils.isNotBlank(medicinesDto.getKeywords()),Medicines::getKeywords,medicinesDto.getKeywords())
            .eq(StringUtils.isNotBlank(medicinesDto.getMedicinesType()),Medicines::getMedicinesType,medicinesDto.getMedicinesType())
            .eq(StringUtils.isNotBlank(medicinesDto.getProducterId()),Medicines::getProducterId,medicinesDto.getProducterId())
            .eq(StringUtils.isNotBlank(medicinesDto.getPrescriptionType()),Medicines::getPrescriptionType,medicinesDto.getPrescriptionType())
            .eq(StringUtils.isNotBlank(medicinesDto.getStatus()),Medicines::getStatus,medicinesDto.getStatus())

        );

        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public Medicines getOne(Long medicinesId) {
        return this.medicinesMapper.selectById(medicinesId);
    }

    @Override
    public int addMedicines(MedicinesDto medicinesDto) {
        Medicines medicines = new Medicines();
        BeanUtil.copyProperties(medicinesDto,medicines);
        medicines.setCreateTime(DateUtil.date());
        medicines.setCreateBy(medicinesDto.getSimpleUser().getUserName());
        return this.medicinesMapper.insert(medicines);
    }

    @Override
    public int updateMedicines(MedicinesDto medicinesDto) {
        Medicines medicines=new Medicines();
        BeanUtil.copyProperties(medicinesDto,medicines);
        medicines.setUpdateBy(medicinesDto.getSimpleUser().getUserName());
        return this.medicinesMapper.updateById(medicines);

    }

    @Override
    public int deleteMedicinesByIds(Long[] medicinesIds) {
        List<Long> ids= Arrays.asList(medicinesIds);
        if(ids.size()>0){
            return this.medicinesMapper.deleteBatchIds(ids);
        }
        return 0;

    }

    @Override
    public List<Medicines> selectAllMedicines() {
        QueryWrapper<Medicines> qw=new QueryWrapper<>();
        qw.eq(Medicines.COL_STATUS, Constants.STATUS_TRUE);
        return this.medicinesMapper.selectList(qw);

    }

    @Override
    public int updateMedicinesStorage(Long medicinesId, Long medicinesStockNum) {
        Medicines medicines=new Medicines();
        medicines.setMedicinesId(medicinesId);
        medicines.setMedicinesStockNum(medicinesStockNum);
        return this.medicinesMapper.updateById(medicines);

    }
}
